import axios from 'axios';
import VueAxios from 'vue-axios'
import { MessageBox } from 'element-ui';
import store from '../vuex/store'
axios.interceptors.request.use(
  config => {
    if (store.state.auth.token) {  // 判断是否存在token，如果存在的话，则每个http header都加上token
      config.headers['callmesoul-token'] = store.state.auth.token;
    }
    return config;
  });
axios.interceptors.response.use(
  response => {
    return response;
  },
  error => {
    if (error.response) {
      if (error.response.status===403){
        MessageBox.alert('请重新登陆', '登陆超时', {
          confirmButtonText: '确定',
          callback: action => {
            store.dispatch("localLogin","/");
          }
        });
        MessageBox.alert('登陆超时',"请重新登陆").then(action => {
          store.dispatch("localLogin","/");
        });
      }
    }
    return Promise.reject(error.response.data);  // 返回接口返回的错误信息
  });

let base="http://api.callmesoul.cn";

let Api={
  //menu
  getMenu:function (params) {
    return axios.get(base+'/api/v1/menus',{params:params});
  },
  createMenu:function (params) {
    return axios.post(base+`/api/v1/menus`,params);
  },
  updateBanner:function (params) {
    console.log(params);
    return axios.post(base+`/banner/index`,params);
  },
  login:function (params) {
    return axios.post(base+`/auth`,params);
  },
  userInfo:function () {
    return axios.post(base+`/api/v1/userInfo`);
  },
  getArticle:function (params) {
    return axios.get(base+`/api/v1/articles`,{params:params});
  },
  createArticle:function (params) {
    return axios.post(base+`/api/v1/articles`,params);
  },
  updateArticle:function (id,params) {
    return axios.put(base+`/api/v1/articles/`+id,params);
  },
  removeArticle:function (id) {
    return axios.delete(base+`/api/v1/articles/`+id);
  }


};


export default Api;
